package com.sihan.swvp.action.wg;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.common.SvgUtil;
import com.sihan.swvp.entity.wg.WgVideo;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.video.VideoManagerService;
import com.sihan.swvp.service.wg.WgDxwgService;
import com.sihan.swvp.service.wg.WgVideoService;
/**
 * 违规视频管理 
 * @author tuf 
 *
 */
@Controller
@RequestMapping("/wgVideoAction")
public class WgVideoAction extends BaseAction {

	@Autowired
	private WgVideoService wgVideoService;
	
	@Autowired
	VideoManagerService vmService;
	@Autowired
	private WgDxwgService wgDxwgService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getWgVideoList")
	public Object getWgVideoList(@Param("content")String content,@Param("vname")String vname,@Param("page")Integer page,@Param("limit")Integer limit){
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("content", content);
		params.put("vname", vname);
		return pageData(wgVideoService.getWgVideoList(params, page, limit));
	}
	/**
	 * 典型违规
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getWgDxwgList")
	public Object getWgDxwgList(){
		return wgDxwgService.getWgDxwg(null, null, null); 
	}
	
	@ResponseBody
	@RequestMapping("/getWgBj")
	public Object getWgBj(@Param("videoId")Integer videoId){
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("videoId", videoId);
		return wgVideoService.getWgBj(params);
	}
	
	@ResponseBody
	@RequestMapping("/getWgBjById")
	public Object getWgBjById(WgVideo bean){
		return wgVideoService.getWgBjById(bean);
	}
	
	/**
	 * 得到二进制图片返回到前台
	 * @param response
	 * @param wid
	 */
	@ResponseBody
	@RequestMapping("/getWgImg")
	public void getWgImg(HttpServletResponse response,WgVideo bean){
		ServletOutputStream output = null;
		FileInputStream in = null;
		try {
			output = response.getOutputStream();
			Map<?,?> data = wgVideoService.getWgImg(bean);
			byte[] bytes = (byte[]) data.get("image");
			output.write(bytes);
		    output.flush();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
		finally{
			CommonUtil.close(output);
			CommonUtil.close(in);
		}
	}
	
	@ResponseBody
	@RequestMapping("/addBj")
	public Object addBj(WgVideo bean,@Param("draw") String draw) {
		try {
			
			Long userId = getLoginUser().getId();
			bean.setUpuser(userId);
			Map<String, Object> validate=wgVideoService.getWgVideoValidate(bean);
			
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("已存在");
			}	
			String  drawdata = draw.replaceAll("“", "\'");
			JSONObject jdraw = JSON.parseObject(drawdata);
			byte[] img = vmService.getVideoPic(CommonUtil.o2int(jdraw.get("videoid")), 
					CommonUtil.o2double(jdraw.get("time")),CommonUtil.o2int(jdraw.get("width")),
					CommonUtil.o2int(jdraw.get("height")));
			byte[] bytes = SvgUtil.draw2png(img,jdraw);
			bean.setImage(bytes);
			wgVideoService.addWgBj(bean);
			logService.addUserLog("添加违规视频", "违规视频", getLoginUser().getId(), "添加违规视频："+bean.getTitle(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加违规视频", "违规视频", getLoginUser().getId(), "添加违规视频："+bean.getTitle(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	} 
	/**
	 * 根据视频id删除标记
	 * @param videoIdArr 视频id
	 * @param addByStr 添加时需删除标记
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteWgBjBatch")
	public Object deleteWgBjBatch(@Param("videoIdArr")Integer[] videoIdArr,String[] videonames){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("videos", Arrays.asList(videoIdArr));
			params.put("wgtime", getParameter("wgtime"));
			Long userId = getLoginUser().getId();
			params.put("upuser", userId);
			String addByStr=getParameter("addByStr");
			if(!"".equals(addByStr) && addByStr !=null){
				params.put("addByStr", addByStr);
			}
			wgVideoService.deleteWgBjBatch(params);
			for(int i=0;i<videonames.length;i++){
				logService.addUserLog("删除违规视频", "违规视频", getLoginUser().getId(), "删除违规视频："+videonames[i],getRemoteAddr(),1,0);
			}
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/delMd")
	public Object delMd(@Param("id")Integer id){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("id", id);
			Long userId = getLoginUser().getId();
			params.put("upuser", userId);
			wgVideoService.deleteWgBjById(params);
			return successMsg();
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg();
		}
		
	}

//	public void base64ToImg(String img64, FileOutputStream out) {
//		try {
//			// 输出文件
//			byte[] bytes = Base64.decode(img64.replace(
//					"data:image/png;base64,", ""));
//			out.write(bytes);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//	}
	
	public File getPrFile(){
		File f = new File("md.properties");
		System.out.println(f.getAbsolutePath());
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return f;
	}
}
